package com.sky.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sky.constant.MessageConstant;
import com.sky.constant.PasswordConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.EmployeeDTO;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.entity.Employee;
import com.sky.exception.AccountLockedException;
import com.sky.exception.AccountNotFoundException;
import com.sky.exception.PasswordErrorException;
import com.sky.holder.EmployeeHolder;
import com.sky.mapper.EmployeeMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.EmployeeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

@Service
public class EmployeeServiceImpl implements EmployeeService {

    public static void main(String[] args) {
        String s = DigestUtils.md5DigestAsHex("123456".getBytes());
        System.out.println(s);
    }

    @Autowired
    private EmployeeMapper employeeMapper;

    /**
     * 员工登录
     *
     * @param employeeLoginDTO
     * @return
     */
    public Employee login(EmployeeLoginDTO employeeLoginDTO) {
        String username = employeeLoginDTO.getUsername();
        String password = employeeLoginDTO.getPassword();

        //1、根据用户名查询数据库中的数据
        Employee employee = employeeMapper.getByUsername(username);

        //2、处理各种异常情况（用户名不存在、密码不对、账号被锁定）
        if (employee == null) {
            //账号不存在
            throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
        }

        //密码比对
        String passwordMd5 = DigestUtils.md5DigestAsHex(password.getBytes());
        if (!passwordMd5.equals(employee.getPassword())) {
            //密码错误
            throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
        }

        if (employee.getStatus() == StatusConstant.DISABLE) {
            //账号被锁定
            throw new AccountLockedException(MessageConstant.ACCOUNT_LOCKED);
        }

        //3、返回实体对象
        return employee;
    }

    @Override
    public Result<String> addEmployee(EmployeeDTO employeeDTO) {
        if (Objects.isNull(employeeDTO)) {
            return Result.error("新增员工失败");
        }
        String username = employeeDTO.getUsername();
        if (StrUtil.isBlank(username)) {
            return Result.error("请输入账号");
        }
        Employee employee = BeanUtil.copyProperties(employeeDTO, Employee.class);
        employee.setPassword(DigestUtils.md5DigestAsHex(PasswordConstant.DEFAULT_PASSWORD.getBytes()));
        employee.setStatus(StatusConstant.ENABLE);
        int result = employeeMapper.saveEmployee(employee);
        if (result < 1) {
            return Result.error("新增失败");
        }
        return Result.success("新增成功");
    }

    @Override
    public Result<PageResult> page(int page, int pageSize, String name) {
        PageHelper.startPage(page, pageSize);
        List<Employee> employees = employeeMapper.queryEmployeeList(name);
        PageInfo<Employee> pageInfo = new PageInfo<>(employees);
        return Result.success(new  PageResult(pageInfo.getTotal(), pageInfo.getList()));
    }

    @Override
    public Result<String> editStatus(Integer available, Long id) {
        if (Objects.isNull(id)) {
            return Result.error("请选择你要更新的用户");
        }
        Employee employee = new Employee();
        employee.setId(id);
        employee.setStatus(available);
        int result = employeeMapper.updateEmployeeStatus(employee);
        if (result < 0) {
            return Result.error("账号状态更改失败！");
        }
        return Result.success("账号状态更改成功！");
    }

    @Override
    public Result<Employee> getEmployeeById(Long id) {
        if (Objects.isNull(id)) {
            return Result.error("请选择你要查询的员工");
        }
        Employee employee = employeeMapper.queryEmployeeId(id);
        return Result.success(employee);
    }

    @Override
    public Result<String> editEmployee(EmployeeDTO employeeDTO) {
        Employee employee = BeanUtil.copyProperties(employeeDTO, Employee.class);
        if (Objects.isNull(employeeDTO.getId())) {
            return Result.error("请选择要修改的用户");
        }
        int result = employeeMapper.updateEmployee(employee);
        if (result < 0) {
            return Result.error("更新失败");
        }
        return Result.success("更新成功");
    }

}
